//
//  BSSettingViewController.m
//  BS
//
//  Created by 王浩宇 on 15/8/20.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BSSettingViewController.h"
#import "BSSettingTableViewCell.h"
#import "SDImageCache.h"
#import "FMDatabase.h"

@interface BSSettingViewController ()
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)NSArray *titleArray;
@property (nonatomic, strong)NSString *string;
@end

@implementation BSSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self uiConfig];
    
    
}

-(void)uiConfig
{
    _dataArray = [[NSMutableArray alloc] init];
    _titleArray = @[@"功能设置", @"其他"];
    
 
                [_dataArray addObject:@[@"字体大小",@"转发时收藏",@"夜间模式"]];
                [_dataArray addObject:@[@"离线下载", @"清除缓存",@"推荐给朋友",@"帮助",@"当前版本",@"关于我们",@"设备信息",@"隐私政策",@"打分支持"]];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    [self changeThemeNotificantion];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    int section = indexPath.section;
//    if (indexPath.section == 0)
//    {
//        static NSString *cellid = @"settingCell";
//        BSSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
//        if (!cell)
//        {
//            cell = [[NSBundle mainBundle] loadNibNamed:@"BSSettingTableViewCell" owner:self options:nil][0];
//        }
//        cell.settingTextLabel.text = _dataArray[indexPath.section][indexPath.row];
//        
//        switch (indexPath.section)
//        {
//            case 0:
//                cell.nightModeSwitch.tag = indexPath.row + 10;
//                break;
//        }
//        
//        if ([@"nt" isEqualToString:[ThemeManager shareInstance].themeName] && cell.nightModeSwitch.tag == 10)
//        {
//            cell.nightModeSwitch.on = YES;
//        }
//        
//        return cell;
//    }

    if (indexPath.section == 0) {
        BSSettingTableViewCell *cell = nil;
         NSString *text =  _dataArray[indexPath.section][indexPath.row];
        if (indexPath.row == 0) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"BSSettingTableViewCell" owner:self options:nil][0];
            cell.fontSize.text = text;

        }
        else{
            cell = [[NSBundle mainBundle] loadNibNamed:@"BSSettingTableViewCell" owner:self options:nil][1];
           
            cell.settingTextLabel.text = text;

        }
     
        return cell;
        
    }

    
    else
    {
        BSSettingTableViewCell *cell3 = nil;
            cell3 = [[NSBundle mainBundle] loadNibNamed:@"BSSettingTableViewCell" owner:self options:nil][2];
        cell3.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell3.restTextLabel.text = _dataArray[indexPath.section][indexPath.row];
            cell3.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell3;
    }
    
    return nil;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray[section] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return _titleArray[section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return;
    }
    switch (indexPath.row)
    {
        case 0:
            NSLog(@"离线下载");
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@""]];
            break;
            
        case 1:
            NSLog(@"清除缓存");
            [self creationAlte];
            break;
        case 2:
            NSLog(@"推荐给朋友");
            break;
            
        case 3:
            NSLog(@"帮助");
            break;
            
        case 4:
            NSLog(@"当前版本");
            break;
        case 5:
            NSLog(@"关于我们");
            break;
        case 6:
            NSLog(@"设备信息");
            break;
        case 7:
            NSLog(@"隐私");
            break;
        case 8:
            NSLog(@"打分支持");
            break;

    }
}

- (void)creationAlte
{
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    NSLog(@"%@",str);
    NSFileManager *fileManager=[NSFileManager defaultManager];
    CGFloat size = [self folderSizeAtPath:str];
    NSString *path = [NSString stringWithFormat:@"缓存大小为%.2fM,确定清除",size];
    UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:path delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [aler show];
    NSLog(@"sflsja:%@",str);
    
}

//单个文件的大小
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

//遍历文件夹获得文件夹大小，返回多少M
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];

    if ([btnTitle isEqualToString:@"确定"]) {
        NSString *str = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSLog(@"-----------:%@",str);
        NSFileManager *fileManager=[NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:str]) {
            NSArray *childerFiles=[fileManager subpathsAtPath:str];
            for (NSString *fileName in childerFiles) {
                //如有需要，加入条件，过滤掉不想删除的文件
                NSString *absolutePath=[str stringByAppendingPathComponent:fileName];
                [fileManager removeItemAtPath:absolutePath error:nil];
            }
        }
        [[SDImageCache sharedImageCache] cleanDisk];
        NSLog(@"点击确定按钮");
    }
}



- (void)creationWebView
{
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
